package com.it.emall.service.impl;

import com.it.emall.domain.AdminUser;
import com.it.emall.domain.Comments;
import com.it.emall.mapper.UserMapper;
import com.it.emall.service.AdminUserService;
import com.it.emall.utils.Md5;
import com.it.emall.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 后台用户Service实现
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @Autowired//把usermapper类型的对象自动注入进来
    private UserMapper userMapper;

    @Override
    public AdminUser login(String username) {
        return userMapper.selectUsername(username);
    }

    @Override
    public AdminUser selectUserById(Integer id) {
        return userMapper.selectUserById(id);
    }

    @Override
    public int updateUserInfo(AdminUser adminUser) {
        return userMapper.updateUserInfo(adminUser);
    }

    @Override
    public int register(AdminUser adminUser) {
        AdminUser byUsername = userMapper.findByUsername(adminUser.getUsername());
        if (byUsername != null) {
            return 0;// 用户名已存在
        }

        adminUser.setPassword(Md5.md5(adminUser.getPassword()));
        return userMapper.registerUser(adminUser);
    }
    //根据用户id查询用户评论
    @Override
    public List<Comments> selectCommentsById(Integer userId) {
        return userMapper.selectCommentsById(userId);
    }
}
